import { expect } from '@jest/globals';

import { initExecuteCommand, runHvigorAsync } from '../../common/common';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  IsEnable,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType, SignType,
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  },
  modulesOption: {
    [ModuleTemplateType.StaticLibrary]: 1,
    [ModuleTemplateType.SharedLibrary]: 1,
  }
};

const testFun: TestCaseFun = async (project, taskConfig, record) => {
  const projectRoot = project.projectRootPath();
  const commands = initExecuteCommand({
    tasks: 'tasks'
  });

  const result = await runHvigorAsync(projectRoot, commands);
  record.addStep('entry tasks', result);

  expect(result.stdout).toContain('All tasks from hvigor node \'entry\'');

  expect(result.stdout).toContain('All tasks from hvigor node \'staticLibrary\'');

  expect(result.stdout).toContain('All tasks from hvigor node \'shareLibrary\'');

  expect(result.stdout).toContain('All tasks from hvigor node \'myapplication\'');

  expect(result.stdout).toContain('Help tasks');
  expect(result.stdout).toContain('tasks - Displays the tasks of the HvigorCoreNode');
  expect(result.stdout).toContain('taskTree - Displays the task dag tree of the HvigorCoreNode');

  expect(result.stdout).toContain('Other tasks');
  expect(result.stdout).toContain('prune - Clean up old hvigor cache files and remove unreferenced packages from store.');
  expect(result.stdout).toContain('clean - Clear the cache information.');
  expect(result.stdout).toContain('collectCoverage - Generate coverage statistics reports based on instrumentation dotting data.');

  expect(result.stdout).toContain('Hook tasks');
  expect(result.stdout).toContain('assembleApp - Assemble the task for the packaged app.');
  expect(result.stdout).toContain('Sync tasks');
  expect(result.stdout).toContain('init');

  expect(result.stdout).toContain('Sign tasks');
  expect(result.stdout).toContain('SignApp - Sign the app package.');

  expect(result.stdout).toContain('Package tasks');
  expect(result.stdout).toContain('PackageApp - Build the app package in the stage model.');

  expect(result.stdout).toContain('Generate tasks');
  expect(result.stdout).toContain('GeneratePackRes - Build the pack.res file in the stage model.');
  expect(result.stdout).toContain('MakeProjectPackInfo - Generate project pack.info in the stage model.');

  expect(result.stdout).toContain('Verification tasks');
  expect(result.stdout).toContain('PreBuildApp - Prepare before building the app.');

  expect(result.stdout).toContain('BUILD SUCCESSFUL');
};

TestCaseBuilder.builder()
  .caseName('hvigorw tasks')
  .description('hvigorw tasks command')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().sign(SignType.NoSign).ohpmInstall(IsEnable.DisAble).caseLevel(CaseLevel.L2).build())
  .testFun(testFun)
  .load();
